#include "StdAfx.h"
#include ".\mfcwpfattributes.h"

CMFCWPFAttributes::CMFCWPFAttributes(void)
{
}

CMFCWPFAttributes::~CMFCWPFAttributes(void)
{
}

///<summary>
///Insert attribute pair into m_mapAttributes
///</summary>
///<param name='szName'>
///the attribute's name
///</param>
///<param name='szValue'>
///the attribute's value
///</param>
///<returns>
///true if successfully inserted; false otherwise
///</returns>
bool CMFCWPFAttributes::InsertAttribute(const CString & szName, const CString & szValue)
{
	CString szNameL = szName;
	CString szValueL = szValue;
	szNameL.MakeLower();
	szValueL.MakeLower();

	if (szNameL == _ATTRIBUTEMARGIN)
	{
		CString sz = szValueL;
		int nIndex = sz.Find(_ATTRIBUTEVALUESEPERATOR);
		if (nIndex == _STRINGNOTFOUND)
		{
			return false;
		}
		CString szTemp = sz.Left(nIndex);
		sz.Delete(0, nIndex+1);
		if (!m_mapAttributes.insert(pair<CString, CString>(_ATTRIBUTELEFT, szTemp)).second)
		{
			return false;
		}

		nIndex = sz.Find(_ATTRIBUTEVALUESEPERATOR);
		if (nIndex == _STRINGNOTFOUND)
		{
			return false;
		}
		szTemp = sz.Left(nIndex);
		sz.Delete(0, nIndex+1);
		if (!m_mapAttributes.insert(pair<CString, CString>(_ATTRIBUTETOP, szTemp)).second)
		{
			return false;
		}

		nIndex = sz.Find(_ATTRIBUTEVALUESEPERATOR);
		if (nIndex == _STRINGNOTFOUND)
		{
			return false;
		}
		szTemp = sz.Left(nIndex);
		sz.Delete(0, nIndex+1);
		if (!m_mapAttributes.insert(pair<CString, CString>(_ATTRIBUTERIGHT, szTemp)).second)
		{
			return false;
		}

		if (!m_mapAttributes.insert(pair<CString, CString>(_ATTRIBUTEBOTTOM, sz)).second)
		{
			return false;
		}
	}
	else if (szNameL == _ATTRIBUTESTROKEDASHARRAY)
	{
		CString sz = szValueL;
		int nIndex = sz.Find(_ATTRIBUTEVALUESEPERATOR);
		if (nIndex == _STRINGNOTFOUND)
		{
			return false;
		}
		CString szTemp = sz.Left(nIndex);
		if (!m_mapAttributes.insert(pair<CString, CString>(_ATTRIBUTESTROKEDASHARRAY0, szTemp)).second)
		{
			return false;
		}
		sz.Delete(0, nIndex+1);
		int nStrokeDashArray0 = _wtoi((wchar_t *)(szTemp.GetString()));

		if (!m_mapAttributes.insert(pair<CString, CString>(_ATTRIBUTESTROKEDASHARRAY1, sz)).second)
		{
			return false;
		}
		int nStrokeDashArray1 = _wtoi((wchar_t *)(sz.GetString()));

		szTemp.Format(_T("%d"), nStrokeDashArray0+nStrokeDashArray1);
		if (!m_mapAttributes.insert(pair<CString, CString>(_ATTRIBUTESTROKEDASHCOUNT, szTemp)).second)
		{
			return false;
		}
	}
	else
	{
		if (!m_mapAttributes.insert(pair<CString, CString>(szNameL, szValueL)).second)
		{
			return false;
		}
	}

	return true;;
}

///<summary>
///get attribute string value according to the given attribute name
///</summary>
///<param name='szName'>
///the attribute's name
///</param>
///<param name='szValue'>
///the attribute's value returned
///</param>
///<returns>
///true if there is the given attribute name in m_mapAttributes; false there is not
///</returns>
bool CMFCWPFAttributes::GetAttribute(const CString & szName, CString & szValue)
{
	bool bReturn = false;
	map<CString, CString>::iterator iter = m_mapAttributes.find(szName);
	if (iter != m_mapAttributes.end())
	{
		szValue = iter->second;
		bReturn = true;
	}
	return bReturn;
}

///<summary>
///get attribute int value according to the given attribute name
///</summary>
///<param name='szName'>
///the attribute's name
///</param>
///<param name='nValue'>
///the attribute's value returned
///</param>
///<returns>
///true if there is the given attribute name in m_mapAttributes; false there is not
///</returns>
bool CMFCWPFAttributes::GetAttribute(const CString & szName, int & nValue)
{
	CString szValue;
	bool bReturn = GetAttribute(szName, szValue);
	if (bReturn)
	{
		nValue = _wtoi((wchar_t *)(szValue.GetString()));
	} 
	return bReturn;
}

///<summary>
///get attribute float value according to the given attribute name
///</summary>
///<param name='szName'>
///the attribute's name
///</param>
///<param name='fValue'>
///the attribute's value returned
///</param>
///<returns>
///true if there is the given attribute name in m_mapAttributes; false there is not
///</returns>
bool CMFCWPFAttributes::GetAttribute(const CString & szName, float & fValue)
{
	CString szValue;
	bool bReturn = GetAttribute(szName, szValue);
	if (bReturn)
	{
		fValue = (float)(_wtof((wchar_t *)(szValue.GetString())));
	} 
	return bReturn;
}

///<summary>
///get attribute bool value according to the given attribute name
///</summary>
///<param name='szName'>
///the attribute's name
///</param>
///<param name='bValue'>
///the attribute's value returned
///</param>
///<returns>
///true if there is the given attribute name in m_mapAttributes; false there is not
///</returns>
bool CMFCWPFAttributes::GetAttribute(const CString & szName, bool & bValue)
{
	CString szValue;
	bool bReturn = GetAttribute(szName, szValue);
	if (bReturn)
	{
		bValue = (szValue == _ATTRIBUTEVALUETRUE) ? true : false;
	}
	return bReturn;
}

///<summary>
///get "items" attribute value
///</summary>
///<param name='szVec'>
///the values returned
///</param>
///<returns>
///true if there is "items" in m_mapAttributes; false there is not
///</returns>
bool CMFCWPFAttributes::GetItems(vector<CString> & szVec)
{
	CString szValue;
	bool bReturn = GetAttribute(_ATTRIBUTEITEMS, szValue);
	if (bReturn)
	{
		int nIndex = 0;
		CString szTemp;
		while (true)
		{
			nIndex = szValue.Find(_ATTRIBUTEVALUESEPERATOR);
			if (nIndex == -1)
			{
				szVec.push_back(szValue);
				break;
			}
			else
			{
				szTemp = szValue.Left(nIndex);
				szValue.Delete(0, nIndex+1);
				szVec.push_back(szTemp);
			}
		}
	}
	return bReturn;
}

///<summary>
///Get color from name
///</summary>
///<param name='szName'>
///color's name
///</param>
///<returns>
///color's value
///</returns>
COLORREF CMFCWPFAttributes::GetColorFromName(const CString & szName)
{
	if (szName == _ATTRIBUTEVALUEBLACK)
	{
		return RGB(0, 0, 0);
	}
	if (szName == _ATTRIBUTEVALUERED)
	{
		return RGB(255, 0, 0);
	}
	if (szName == _ATTRIBUTEVALUEGREEN)
	{
		return RGB(0, 255, 0);
	}
	if (szName == _ATTRIBUTEVALUEBLUE)
	{
		return RGB(0, 0, 255);
	}
	if (szName == _ATTRIBUTEVALUEGRAY)
	{
		return RGB(128, 128, 128);
	}
	return RGB(255, 255, 255);
}